
import TSMap from "../math/TSMap";
import ResLoader from "../res/ResLoader";
import AudioManager from "./AudioManager";
import ResItem from "../res/ResItem";
export default  abstract class BaseAudioManager {

    static sourcePool:TSMap  = new TSMap();

    static MAX_NUM:number = cc.audioEngine.getMaxAudioInstance();

    private loader:ResLoader = null;

    protected audioManager:AudioManager;

    constructor(loader:ResLoader){
        // this.dir = dir;
        this.loader = loader;
        this.audioManager = AudioManager.instance();
       
       
    }

    clear(){
        
    }
    
    play(name:string,isLoop?:boolean){
        if(!this.audioManager.getMusicFlag()){
            return;
        }
        // name = this.getDir() + name;
        if(BaseAudioManager.sourcePool.size() < BaseAudioManager.MAX_NUM){    
                this.loader.loadRes(name,cc.AudioClip,(err,item:ResItem)=>{
                    if(err){
                        //console.log("AudioSource err ",err)
                        cc.log("AudioSource err ",err);
                        return ;
                    }

                    this.add(name,item.getRes(),isLoop);
                });
            }
    }

    abstract add(name,res,isLoop);

    abstract pause(name?:string);

    abstract resume(name?:string);

    abstract stop(name?:string);

    abstract setVolume(count:number);

    abstract isPlaying():boolean;

    static pauseAll(){
        cc.audioEngine.pauseAll();
    }
    static resumeAll(){
        cc.audioEngine.resumeAll();
    }
    static stopAll () {
        cc.audioEngine.stopAll();
        BaseAudioManager.sourcePool.clear();
    } 
}
